Repository navigation
test(api,trpc): replace module mocks with stubs scoped to one file - #8315
Conversation
mock.module is process-wide in bun and has no undo. Without --isolate, a partial fake of ./processDelivery, @superset/db/client and others reached later test files (processDelivery, recordWebhookDelivery and connector refresh tests failed, depending on order). Each file now swaps members of the real module or object in beforeAll and restores them in afterAll. apps/api gets a test preload with fixed env values, so the @/env mocks are gone. No production code changes.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configuration
📒 Files selected for processing (32)
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 4 remain after this review. 📝 WalkthroughWalkthroughThe pull request adds test environment setup and reusable stubbing helpers for the API and tRPC packages. It converts API and tRPC tests from process-wide module mocks to stubs on imported modules while retaining the described fixture behavior. ChangesTest setup and stubbing
Priority: ⬇️ Low Estimated code review effort: 3 (Moderate) | ~25 minutes Change: Other Suggested reviewers: Merge Risk: ⚪ Minimal · up to No confirmed issue blocks merging after normal checks. The credential test’s environment timing remains unverified. 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
Full details: Docstring CoverageExplanation Docstring coverage is 57.14% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 7 functions across 30 files. (2 skipped: 2 unsupported.)
✨ Finishing Touches 💡 1📝 Generate docstrings 💡
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
🧹 Preview Cleanup CompleteThe following preview resources have been cleaned up:
Thank you for your contribution! 🎉 |
|
Preview update published for development build |
|
* test: run suites without --isolate and remove three flaky tests The cross-file leaks that --isolate hid are fixed (#8314, #8315, #8316, #8319), so each package's suite now runs in one process. The desktop suite takes about 38 s without the flag and about 73 s with it. apps/relay still had one leak: host-tunnel.test.ts replaced partyserver with a stub that has no getServerByName, which broke index.test.ts in some file orders. The test passes against the real module, so the stub is gone. Removed as flaky: - workspace-fs: FsWatcherManager resumes a subscription after the root is deleted and recreated - i18n: locale changes preserve state and focus (provider-state) - agent-setup: Pi completion notifications, which poll up to 1 s for a detached script * test(trpc): keep the opt-in Postgres watch test out of the shared suite It sets DATABASE_URL and then imports the db client. In one process the client may already exist, so the test would write its fixtures to the database the first file connected to. As an .integration.ts file it runs only when named, in its own process, like the other opt-in suites.
Part of removing cross-file test leaks so suites do not depend on
bun test --isolate.mock.moduleis process-wide in bun and has no undo: without--isolate, a fake reaches every test file that loads later.What changed
All 59
mock.modulecalls inapps/apitests and 24 inpackages/trpctests are gone. No production code changed, so no tRPC contract changed.test/stub.tsexportsstub(target, fakes): it swaps members of a live object or module namespace inbeforeAll(withspyOnfor functions) and restores them inafterAll. Exports that a file does not list stay real, so the "list every export" partial fakes are gone.apps/apihad no test preload. It now hasbunfig.tomlandtest/setup-env.tswith placeholders and the fixed values the tests assert on, which removes the six@/envmocks.run-agent.test.tsthrows the real Anthropic error classes.This is the same amount of faking as before, scoped to one file. It does not inject dependencies: passing
dbthrough every router and route handler is a production change this PR does not make.Leaks fixed
linear/webhook/route.test.tsprocessDelivery.test.ts./processDelivery@superset/db/clientrecordWebhookDelivery.test.tsand othersdbreached later filesplugins/proxy/resolve-target.test.tsconnectors/refresh.test.tsslack/events/route.test.ts(in-file)mockImplementationOncecarried into the next testplugins/proxy/plugin-server.test.ts(in-file).envsandbox/credentials.test.tsNEXT_PUBLIC_API_URLwas unsetNo test was deleted. api runs 230 tests (some files used to fail at load without
--isolate), trpc 570.Known limits
test/stub.tsis two identical files, one per package.public-access.test.tsused to trap every access todb; it now trapsquery,select,insert,update,delete,executeandtransaction.*.integration.tsfiles inpackages/trpcstill callmock.module("../../lib/r2").bun testdoes not match them.Verification
bun 1.4.2,
bun test --randomize --seed N, seeds 1-12, in both packages: all pass without--isolateand with it. Also run with the root.envneutralised, to match CI. Repo typecheck and biome pass.Summary by cubic
Replaces bun's process-wide and irreversible
mock.modulewith per-file stubs so test suites no longer depend onbun test --isolate. Amock.modulefactory in one test file leaked to every test file loaded later and could not be undone; the newstubhelper swaps members of the live module or object inbeforeAlland restores them inafterAll. No production code or tRPC contract changed, and no tests were deleted (api: 230, trpc: 570).Changes
test/stub.tsper package; partial "list every export" fakes are gone because exports a file does not stub stay real.apps/apigained abunfig.tomlpreload andtest/setup-env.tswith fixed env values, removing the six@/envmocks.run-agent.test.tsnow throws real Anthropic error classes.mockImplementationOnceinslack/events/route.test.tsand cached account layouts by plugin version inplugin-server.test.ts.Known limits
test/stub.tsis duplicated across the two packages.public-access.test.tstraps a subset ofdboperations instead of every property access.*.integration.tsfiles inpackages/trpcstill callmock.module;bun testdoes not match them.Verified with
bun test --randomize --seed 1..12in both packages, with and without--isolate, and with the root.envneutralized; repo typecheck and biome pass.Written for commit f82a83b. Summary will update on new commits.
Summary by CodeRabbit